package com.kexinde.wjifen.util;

import java.util.Properties;

import com.opensymphony.oscache.base.EntryRefreshPolicy;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.base.persistence.CachePersistenceException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
import com.opensymphony.oscache.web.filter.ExpiresRefreshPolicy;

/**
 * oscahce，用于缓存对象
 * @author vincent
 *
 */
public class OSCache {
 /**
  *通用缓存管理类 administrator
  */
 private GeneralCacheAdministrator cache;

 private static OSCache instance;

 public OSCache() {
  this.cache = new GeneralCacheAdministrator();
 }

 /**
  * 指定加载加载cache.propeties fiel 默认情况下加载classes目录下的OScache.properties
  * 
  * @param prop
  */
 public OSCache(Properties prop) {
  this.cache = new GeneralCacheAdministrator(prop);
 }

 /**
  * 返回OSCache 的Instance 单态模式
  * 
  * @return
  */
 public synchronized static OSCache getInstance() {

  if (instance == null) {
   instance = new OSCache();
  }
  return instance;
 }

 /**
  * 设置缓存容量 default value 请查看oscache配置文件，OSCache系统本身默认值unlimited
  * 
  * @param cacheCapacity
  */
 public void setCacheCapacity(int cacheCapacity) {
  this.cache.setCacheCapacity(cacheCapacity);
 }

 /**
  * 根据Key获取cache里缓存的object
  * 
  * @param key
  *            查找关键字
  */
 public Object get(Object key) throws CachePersistenceException {
  try {
   return this.cache.getFromCache(String.valueOf(key));
  } catch (NeedsRefreshException e) {
   cache.cancelUpdate(String.valueOf(key));
   return null;
  }
 }

 /**
  * 根据key和time获取缓存的object
  * 
  * @param key
  *            查找的key
  * @param time
  *            (最准确的含义-->)How long the object can stay in cache in seconds
  */
 public Object get(Object key, int time) throws CachePersistenceException {
  try {
   return this.cache.getFromCache(String.valueOf(key), time);
  } catch (NeedsRefreshException e) {
   cache.cancelUpdate(String.valueOf(key));
   return null;
  }
 }

///**
//* 尽量不要使用该方法 根据time 获取key
//* 
//* @param time
//*            时间
//*/
//public Object getkey(int time) throws CachePersistenceException {
//String key = RandomStringUtils.randomAlphanumeric(10);
//try {
//while (this.cache.getFromCache(key) != null) {
// key = RandomStringUtils.randomAlphanumeric(10);
//}
//return key;
//} catch (NeedsRefreshException e) {
//return key;
//}
//}

 /**
  * 缓存对象
  */
 public void inputOSCache(Object key, Object value)
   throws CachePersistenceException {
  this.cache.putInCache(String.valueOf(key), value);
 }

 /**
  * 缓存对象
  * 
  * @param key
  *            缓存对象的key
  * @param value
  *            缓存对象的value
  * @param n
  *            缓存对象有效时间
  */
 public void inputOSCache(Object key, Object value, int n)
   throws CachePersistenceException {
  EntryRefreshPolicy Policy = new ExpiresRefreshPolicy(n);
  this.cache.putInCache(String.valueOf(key), value, Policy);
 }


 /**
  * 根据key从cache里 删除object
  * 
  * @param 要删除缓存对象的key
  */
 public void remove(Object key) throws CachePersistenceException {
  this.cache.flushEntry(String.valueOf(key));
 }

 /**
  * 清空所有的缓存
  */
 public void clear() throws CachePersistenceException {
  this.cache.flushAll();
 }

 /**
  * 销毁缓存
  */
 public void destroy() throws CachePersistenceException {
  this.cache.destroy();
 }

}